package com.whacos.app.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whacos.common.utils.BeanUtils;
import com.whacos.common.utils.DateUtils;
import com.whacos.common.utils.ObjectUtils;
import com.whacos.common.utils.StringUtils;
import com.whacos.modules.blog.entity.Content;
import com.whacos.modules.blog.service.ContentService;
import com.whacos.modules.blog.vo.ContentVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author xiongdun
 * @description 文章内容
 * @since 2018/11/17 17:11
 */
@Controller
@RequestMapping("/app/blog")
public class AppBlogController {

    private static final Logger logger = LoggerFactory.getLogger(AppBlogController.class);

    @Autowired
    private ContentService contentService;

    @GetMapping()
    public String blog() {
        return "blog/index/main";
    }

    @ResponseBody
    @GetMapping("/list")
    public IPage<ContentVO> list(ContentVO contentVO) {
        IPage<ContentVO> contentVOPage = new Page<>();
        try {
            // 查询数据
            IPage<Content> contentPage = contentService.page(new Page<>(contentVO.getPageNum(), contentVO.getPageSize()),
                    new LambdaQueryWrapper<Content>()
                            .eq(StringUtils.isNotEmpty(contentVO.getType()), Content::getType, contentVO.getType())
                            .eq(StringUtils.isNotEmpty(contentVO.getTitle()), Content::getTitle, contentVO.getTitle())
                            .eq(StringUtils.isNotEmpty(contentVO.getCategories()), Content::getCategories, contentVO.getCategories())
                            .eq(StringUtils.isNotEmpty(contentVO.getAuthor()), Content::getAuthor, contentVO.getAuthor())
                            .eq(ObjectUtils.isNotEmpty(contentVO.getStatus()), Content::getStatus, contentVO.getStatus())
                            .orderByDesc(Content::getUpdatedDate));
            // 返回结果
            BeanUtils.copyPage(contentPage, contentVOPage);
        } catch (Exception e) {
            logger.error("查询博客内容列表异常，异常原因：" + e.getMessage(), e);
        }
        return contentVOPage;
    }

    @GetMapping("/post/{id}")
    public String post(@PathVariable("id") Long id, Model model) {
        Content content = contentService.getById(id);
        model.addAttribute("content", content);
        model.addAttribute("updateDate", content.getUpdatedDate().toString());
        return "blog/index/post";
    }

    @GetMapping("/page/{categories}")
    public String about(@PathVariable("categories") String categories, Model model) {
        try {
            // 封装查询条件
            QueryWrapper<Content> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(Content::getCategories, categories);
            Content content = new Content();
            if (contentService.count(queryWrapper) > 0L) {
                content = contentService.list(queryWrapper).get(0);
            }
            model.addAttribute("content", content);
        } catch (Exception e) {
            logger.error("查询博客信息异常，异常原因：" + e.getMessage(), e);
        }
        return "blog/index/post";
    }
}
